package com.jinxin.platform.cdinfogo.service.impl;

import com.jinxin.platform.cdinfogo.mapper.CdinfogoDcMapper;
import com.jinxin.platform.cdinfogo.pojo.domain.CdinfogoContent;
import com.jinxin.platform.cdinfogo.mapper.CdinfogoContentMapper;
import com.jinxin.platform.cdinfogo.pojo.domain.CdinfogoDc;
import com.jinxin.platform.cdinfogo.service.CdinfogoContentService;
import org.springframework.stereotype.Service;
import com.jinxin.platform.base.common.pojo.JsonResult;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (CdinfogoContent)表服务实现类
 *
 * @author zhangpeng
 * @since 2021-06-28 17:23:22
 */
@Service("cdinfogoContentService")
public class CdinfogoContentServiceImpl implements CdinfogoContentService {
    @Resource
    private CdinfogoContentMapper cdinfogoContentMapper;
    @Resource
    private CdinfogoDcMapper cdinfogoDcMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public JsonResult queryById(String id) {
        CdinfogoContent cdinfogoContent = this.cdinfogoContentMapper.queryById(id);
        return JsonResult.ok(cdinfogoContent);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param cdinfogoContent 实例对象
     * @return 对象列表
     */
    @Override
    public JsonResult queryAll(CdinfogoContent cdinfogoContent) {
        List<CdinfogoContent> cdinfogoContentList =  this.cdinfogoContentMapper.queryAll(cdinfogoContent);
        return JsonResult.ok(cdinfogoContentList);
    }

    /**
     * 新增数据
     *
     * @param cdinfogoContent 实例对象
     * @return 实例对象
     */
    @Override
    public JsonResult insert(CdinfogoContent cdinfogoContent) {
        int i = this.cdinfogoContentMapper.insert(cdinfogoContent);
        return i > 0 ? JsonResult.ok("新增成功",cdinfogoContent) : JsonResult.error("新增失败！");
    }

    /**
     * 修改数据
     *
     * @param cdinfogoContent 实例对象
     * @return 实例对象
     */
    @Override
    public JsonResult update(CdinfogoContent cdinfogoContent) {
        int i = this.cdinfogoContentMapper.update(cdinfogoContent);
        return i > 0 ? JsonResult.ok("修改成功！",null) : JsonResult.error("修改失败！");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public JsonResult deleteById(String id) {
        return this.cdinfogoContentMapper.deleteById(id) > 0 ? JsonResult.ok("删除成功！",null) : JsonResult.error("删除失败！");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public JsonResult insertAll(List<CdinfogoContent> cdinfogoContents, List<String> ids) {

        ids.forEach( id -> { //循环设备
            //通过设备id获取到相应的发布内容
            List<CdinfogoDc> cdinfogoDcs = cdinfogoDcMapper.queryAll(CdinfogoDc.builder().deviceId(id).build());
            for (CdinfogoDc c: cdinfogoDcs) {
                CdinfogoContent cdinfogoContent = cdinfogoContentMapper.queryById(c.getContentId());
                //查找对应的发布类型删除
                cdinfogoContents.forEach( cent -> {
//                    if ("2".equals(cent.getShowType()) || "3".equals(cent.getShowType())){
//                        cdinfogoContentMapper.deleteById(cdinfogoContent.getId());
//                        cdinfogoDcMapper.deleteById(c.getId());
//                    }
                    if(cdinfogoContent.getShowType().equals(cent.getShowType())){
                        cdinfogoContentMapper.deleteById(cdinfogoContent.getId());
                        cdinfogoDcMapper.deleteById(c.getId());
                    }
                });
            }
        });

        //循环每个发布内容添加到设备
        cdinfogoContents.forEach( c -> {
            cdinfogoContentMapper.insert(c);
            ids.forEach( id -> {
                CdinfogoDc build = CdinfogoDc.builder().contentId(c.getId()).deviceId(id).build();
                cdinfogoDcMapper.insert(build);
            });
        });

        return JsonResult.ok();
    }
}